layui.define(["jquery", "layer"], function (exports) {
    var $ = layui.$,
        layer = layui.layer;
    var baseUrl = '/api';              //此处填写您的域名地址

    /**token */
    const token = localStorage.getItem("token");      //获取存储在浏览器中的token
    let headers;
    if (token) {
        headers = {
            "Authorization": `Bearer ${token}`             //关于请求头
        }
    }

    var ajax = {
        /**get请求数据 */
        get: function (url = '', params = {}, isLoading = false) {
            if (isLoading) {
                layer.load(1)          //在请求未响应之前,给用户一个等待状态
            }
            var promise = new Promise((resolve, reject) => {
                $.ajax({
                    url: baseUrl + url,
                    type: 'GET',
                    data: params,
                    headers,
                    success: res => {
                        if (isLoading) {
                            layer.closeAll('loading')
                        }
                        if (res.code == 404) {
                            layer.msg(res.msg, { icon: 7 })
                            reject(res.data)
                        } else if (res.code == 401) {
                            layer.msg('登录过期，请重新登录', { icon: 7 })
                            setTimeout(() => {
                                localStorage.clear();
                                window.location.href = 'page/login.html'        //根据后端返回code,判断一些错误情况
                            }, 1500);
                        } else if (res.code == 402) {
                            layer.msg('您无权限访问此系统', { icon: 7 })
                            setTimeout(() => {
                                localStorage.clear();
                                window.location.href = 'page/login.html'
                            }, 1500);
                        } else {
                            resolve(res)
                        }
                    },
                    error: err => {
                        layer.msg(err, { icon: 7 })       //其他错误情况
                        reject(err)
                    }
                })
            })
            return promise
        },
        /**post */
        post: function (url = '', params = {}, isLoading = false) {
            if (isLoading) {
                layer.load(1)
            }
            var promise = new Promise((resolve, reject) => {
                $.ajax({
                    url: baseUrl + url,
                    type: 'POST',
                    data: params,
                    headers,
                    success: res => {
                        if (isLoading) {
                            layer.closeAll('loading')
                        }
                        if (res.code == 404) {
                            layer.msg(res.msg, { icon: 7 })
                            reject(res.data)
                        } else if (res.code == 401) {
                            layer.msg('登录过期，请重新登录', { icon: 7 })
                            setTimeout(() => {
                                localStorage.removeItem('token')
                                window.location.href = 'page/login.html'
                            }, 1500);
                        } else if (res.code == 402) {
                            layer.msg('您无权限访问此系统', { icon: 7 })
                            setTimeout(() => {
                                localStorage.removeItem('token')
                                window.location.href = 'page/login.html'
                            }, 1500);
                        } else {
                            resolve(res)
                        }
                    },
                    error: err => {
                        layer.msg(err, { icon: 7 })
                        reject(err)
                    }
                })
            })
            return promise
        }
    }
    exports("ajax", ajax);
})